/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2007 University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef TEXCPQUEUE_H
#define TEXCPQUEUE_H

#include <queue>
#include <fstream>
#include "ns3/packet.h"
#include "ns3/queue.h"
#include "ns3/data-rate.h"
#include "ns3/timer.h"
#include "ns3/traced-value.h"
#include "texcp-header.h"

namespace ns3 {

class TraceContainer;

namespace texcp_routing {
/**
 * \ingroup queue
 *
 * \brief A drop-tail queue. Texcp probes are updated with the node information
 */

class TexcpQueue : public Queue {

public:
  static TypeId GetTypeId (void);
  /**
   * \brief TexcpQueue Constructor
   *
   * Creates a droptail queue with a maximum size of 100 packets by default
   */
  TexcpQueue ();
  TexcpQueue (std::string rate); 
  virtual ~TexcpQueue();

  /**
   * Enumeration of the modes supported in the class.
   *
   */
  enum Mode {
    ILLEGAL,     /**< Mode not set */
    PACKETS,     /**< Use number of packets for maximum queue size */
    BYTES,       /**< Use number of bytes for maximum queue size */  
  };

  /**
   * Set the operating mode of this device.
   *
   * \param mode The operating mode of this device.
   *
   */
  void SetMode (TexcpQueue::Mode mode);

  /**
   * Get the encapsulation mode of this device.
   *
   * \returns The encapsulation mode of this device.
   */
  TexcpQueue::Mode  GetMode (void);
  
  void SetOutput(std::string filename); 
  void TxPathUtilization (double u);

  void ResetStatistics (void);
  void DoStart(void);
private:
  virtual bool DoEnqueue (Ptr<Packet> p);
  virtual Ptr<Packet> DoDequeue (void);
  virtual Ptr<const Packet> DoPeek (void) const;
  
  bool IsExistingIEPair(IEPair ie);
  std::ofstream ascii;

  TexcpHeader SetProbe (TexcpHeader probe);
  std::queue<Ptr<Packet> > m_packets;
  uint32_t m_maxPackets;
  uint32_t m_maxBytes;
  uint32_t m_bytesInQueue;
  Mode     m_mode;
  
  TracedValue<double> m_pathUtilization;
  uint32_t m_bytesPrevInQueue;
  double m_aggregateFeedback;
  DataRate m_bps;
  Time m_probeInt;
  
  int m_nbrIEPairs;//number of Active IE pairs during the last observation period
  std::list<IEPair> m_observedIEPairs;// List of IE pairs observed since the data has been reset
  Timer m_iePairsTimer;
  void IEPairsTimerExpire(void);

  Timer m_texcpUpdateTimer;
  void TexcpUpdateTimerExpire(void);
  
  //For a simpla way of logging an id is attributed to the queue
  //Should be deleted later
  static int m_nbrQueues;
  static int Next(){
  m_nbrQueues ++;
  return m_nbrQueues;
  }
  int id;

  TracedValue<double> m_loss;
};
}}; // namespace ns3

#endif /* TEXCPQUEUE_H */
